package org.wdj.utils;

import org.springframework.context.ApplicationContextAware;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.BeansException;


@Component
public class EmailUtil implements ApplicationContextAware {
    private static JavaMailSender javaMailSender;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        javaMailSender = applicationContext.getBean(JavaMailSender.class);
    }

    public static void send(String email, String username, String code) {
        // 创建一个新的 SimpleMailMessage 对象
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("230720986@qq.com"); // 发件人
        message.setText("尊敬的"+ username + " 您的验证码是: " + code + "，请妥善保管。");
        message.setSubject("验证码");
        message.setTo(email); // 收件人

        // 发送邮件
        javaMailSender.send(message);
    }
}

